var app = getApp()
var shopModel = require("../../model/shopModel.js")
var util = require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    currentType: '分类',
    typeTags: ['全部', '商品', '服务'],
    saleTags: ['从高到低', '从低到高'],
    priceTags: ['从高到低', '从低到高'],
    typeIndex: 0,
    saleIndex: 0,
    priceIndex: 0,
    sortType: 0, //0:销量 1:价格
    sort: 0, //0:从高到低 1:从低到高
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  onShow: function() {
    this.allGoods()
  },
  switchTypeTag(e) {
    var index = e.currentTarget.id
    this.setData({
      typeIndex: index,
      isShowType: false,
      currentType: this.data.typeTags[index]
    })
    this.getData();
  },
  //销量标签
  switchSaleTag(e) {
    var index = e.currentTarget.id
    this.setData({
      saleIndex: index,
      isShowSale: false,
      sort: index,
      sortType: 0
    })
    this.getData();
  },
  //价格标签
  switchPriceTag(e) {
    var index = e.currentTarget.id
    this.setData({
      priceIndex: index,
      isShowPrice: false,
      sort: index,
      sortType: 1
    })
    this.getData();
  },
  //分类标签窗口
  bindShowType() {
    this.setData({
      isShowType: !this.data.isShowType,
      isShowSale: false,
      isShowPrice: false
    })
  },
  //销量标签窗口
  bindShowSale() {
    this.setData({
      isShowSale: !this.data.isShowSale,
      isShowType: false,
      isShowPrice: false
    })
  },
  //价格标签窗口
  bindShowPrice() {
    this.setData({
      isShowPrice: !this.data.isShowPrice,
      isShowType: false,
      isShowSale: false
    })
  },
  //加载数据
  getData() {
    switch (parseInt(this.data.typeIndex)) {
      case 0:
        this.allGoods()
        break
      case 1:
        this.getProduct()
        break
      case 2:
        this.getService()
        break
    }
  },
  //获取所有类型产品
  allGoods() {
    var that = this
    var data = {
      keyword: this.data.keyword,
      sortType: this.data.sortType,
      sort: this.data.sort
    }
    util.showLoading("加载中")
    shopModel.allGoods(data, res => {
      wx.hideLoading()
      that.setData({
        goodsList: res.data
      })
    })
  },
  //产品类
  getProduct() {
    var that = this
    var data = {
      keyword: this.data.keyword,
      sortType: this.data.sortType,
      sort: this.data.sort
    }  
     util.showLoading("加载中")
    shopModel.getProduct(data, res => {
      wx.hideLoading()
      that.setData({
        goodsList: res.data
      })
    })
  },
  //服务类
  getService() {
    var that = this
    var data = {
      keyword: this.data.keyword,
      sortType: this.data.sortType,
      sort: this.data.sort
    }
    util.showLoading("加载中")
    shopModel.getService(data, res => {
      wx.hideLoading()
      that.setData({
        goodsList: res.data
      })
    })
  },
  //搜索确认
  bindSearchConfirm(e) {
    var val = e.detail.value
    var keyword = this.data.keyword
    if (keyword == val) return
    this.setData({
      keyword: val
    })
    this.getData()
  },
  //详情
  navToInfo(e) {
    var data=e.currentTarget.dataset.data
    wx.navigateTo({
      url: '../goods_info/goods_info?id='+data.id+"&type="+data.type,
    })
  }
})